package edu.jd.authoritymanagement;

import edu.jd.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AuthorityService {

    public List<Authority> getAllAuthorities(){
        SqlSession sess = Utils.openSession();
        try{
            AuthorityDao dao = sess.getMapper(AuthorityDao.class);
            List<Authority> authList = dao.getAuthorities();
            sess.commit();
            return authList;
        }catch (Exception e){
            e.printStackTrace();
            sess.rollback();
            throw new RuntimeException("获取权限信息失败");
        }finally {
            sess.close();
        }
    }

    public List<Authority> getAuthorityByRoleId(int role_id){
        SqlSession sess = Utils.openSession();
        try{
            AuthorityDao dao = sess.getMapper(AuthorityDao.class);
            List<Authority> authList = dao.getAuthoritiesByRoleId(role_id);
            sess.commit();
            return authList;
        }catch (Exception e){
            e.printStackTrace();
            sess.rollback();
            throw new RuntimeException("获取权限信息失败");
        }finally {
            sess.close();
        }
    }

    public List<Integer> getRoleIdByAuthId(int auth_id){
        SqlSession sess = Utils.openSession();
        try{
            AuthorityDao dao = sess.getMapper(AuthorityDao.class);
            List<Integer> roleList = dao.getRoleByAuthId(auth_id);
            sess.commit();
            return roleList;
        }catch (Exception e){
            e.printStackTrace();
            sess.rollback();
            throw new RuntimeException("获取权限信息失败");
        }finally {
            sess.close();
        }
    }
}
